#ifndef OSIFACE_H_INCLUDED
#define OSIFACE_H_INCLUDED
#include "leaf/base.h"
#include <avrx.h>
#include <avr/io.h>
#include <avr/interrupt.h>

/*+ 内核资源分配 **********************************************************************/
static inline void set_kernel_stack( void *sp )
{
	AvrXSetKernelStack( sp );
}

/*+ 中断控制与内核进出 ****************************************************************/
static inline void enter_kernel( void )
{
	IntProlog();
}
static inline void enter_task( void )
{
	Epilog();
}
static inline void exit_kernel( void )
{
	Epilog();
}
#define DEF_OSISR( intr ) \
	AVRX_SIGINT( intr )
#define DEF_ISR( intr ) \
	ISR( intr )

/*+ 任务管理 *************************************************************************/
typedef TaskControlBlock	Task;/*TaskControlBlock实际上保存的是一个任务的静态描述*/
typedef pProcessID			Pid;/* pid是一进程的标识，所以定义为指针比较合理 */
typedef ProcessID			Process;/* ProcessID结构保存的是一个进程在运行中的信息 */

#define DECL_TASK( task, sz, prio )\
	AVRX_GCC_TASK( task, sz, prio )

#define DEF_TASK( task, sz, prio ) \
	AVRX_GCC_TASKDEF( task, sz, prio )

#define DECL_TASK_EXT( task ) \
	AVRX_EXTERNTASK( task )

static inline void execute( Task * task )
{
	AvrXRunTask( task );
}
static inline void init_task( Task * task )
{
	AvrXInitTask( task );
}
static inline void resume( Pid p )
{
	AvrXResume( p );
}
static inline void suspend( Pid p )
{
	AvrXSuspend( p );
}
#define load_task( tasknm ) init_task( &tasknm##Tcb )
#define execute_task( tasknm ) execute( &tasknm##Tcb )
#define resume_task( tasknm ) resume( &tasknm##Pid )
#define suspend_task( tasknm ) suspend( &tasknm##Pid )
static inline void kill( Pid p )
{
	AvrXTerminate( p );
}
static inline void task_exit( void )
{
	AvrXTaskExit();
}
static inline void system_halt( void )
{
	AvrXHalt();
}
static inline uint8_t task_get_prio( Pid p )
{
	return AvrXPriority( p );
}
static inline uint8_t task_set_prio( Pid p, uint8_t prio )
{
	return AvrXChangePriority( p, prio );
}
static inline Pid self( void )
{
	return AvrXSelf();
}
void AvrXYield(void);
static inline void task_yield( void )
{
	AvrXYield();
}
/*+  信号量 *************************************************************************/
//typedef Mutex				Mutex;
//typedef	pMutex				pMutex;
static inline void set_mutex( pMutex m )
{
	AvrXSetSemaphore( m );
}
static inline void set_mutex_int( pMutex m )
{
	AvrXIntSetSemaphore( m );
}
static inline void wait_mutex( pMutex m )
{
	AvrXWaitSemaphore( m );
}
static inline Mutex test_mutex( pMutex m )
{
	return AvrXTestSemaphore( m );
}
static inline void reset_mutex( pMutex m )
{
	AvrXResetSemaphore( m );
}

/*+  定时系统 ***********************************************************************/
typedef TimerControlBlock	Timer;
typedef Timer *				pTimer;
typedef unsigned int		Ticks;

#define time_tick AvrXTimerHandler
static inline void start_timer( pTimer tmr, Ticks ticks )
{
	AvrXStartTimer( tmr, ticks );
}
static inline void wait_timer( pTimer tmr )
{
	AvrXWaitTimer( tmr );
}
static inline void sleep_ticks( pTimer tmr, Ticks ticks )
{
	AvrXDelay( tmr, ticks );
}

/*+ 消息系统 ************************************************************************/
typedef MessageQueue		MsgBox;
typedef pMessageQueue		pMsgBox;
typedef MessageControlBlock	Message, Msg;
typedef Message *			pMsg;
typedef TimerMessageBlock	TimerMsg;
typedef TimerMsg *			pTimerMsg;
static inline void start_timer_msg( pMsgBox mb, TimerMsg *tm, unsigned delay )
{
	AvrXStartTimerMessage( tm, delay, mb );
}
static inline void cancel_timer_msg( pMsgBox mb, TimerMsg *tm )
{
	AvrXCancelTimerMessage( tm, mb );
}
static inline void send_msg_int( pMsgBox mb, pMsg m )
{
	AvrXIntSendMessage( mb, m );
}
static inline void send_msg( pMsgBox mb, pMsg m )
{
	AvrXSendMessage( mb, m );
}
static inline pMsg wait_msg( pMsgBox mb )
{
	return AvrXWaitMessage( mb );
}
static inline pMsg check_msg_box( pMsgBox mb )
{
	return AvrXRecvMessage( mb );
}
static inline void ack_msg( pMsg m )
{
	AvrXAckMessage( m );
}
static inline void wait_msg_ack( pMsg m )
{
	AvrXWaitMessageAck( m );
}
static inline Mutex test_msg_ack( pMsg m )
{
	return AvrXTestMessageAck( m );
}

/*+ System Message Utility */
typedef union{
	void *p;
	int val;
}SysMsgData;
typedef struct{
	Message mcb;
	uint8_t id;
	union{
		void *p;
		int val;
	}data;
}SysMsg, *pSysMsg;
static inline void send_sys_msg( pMsgBox mb, pSysMsg msg )
{
	send_msg( mb, (pMsg)msg );
}
static inline void send_sys_msg_int( pMsgBox mb, pSysMsg msg )
{
	send_msg_int( mb, (pMsg)msg );
}
static inline pSysMsg wait_sys_msg( pMsgBox mb )
{
	return ( pSysMsg )wait_msg( mb );
}
static inline void wait_sys_msg_ack( pSysMsg m )
{
	wait_msg_ack( &m->mcb );
}
static inline void ack_sys_msg( pSysMsg m )
{
	ack_msg( &m->mcb );
}
typedef void ( * SysMsgHandler ) ( pSysMsg );
typedef void ( * TimeoutHandler )( void );
#endif /* OSIFACE_H_INCLUDED */
